package cn.waqwb.bwcar.controller;

import cn.waqwb.bwcar.dto.DataGridResult;
import cn.waqwb.bwcar.dto.QueryDTO;
import cn.waqwb.bwcar.pojo.Car;
import cn.waqwb.bwcar.pojo.CarMake;
import cn.waqwb.bwcar.service.CarMakeService;
import cn.waqwb.bwcar.service.CarService;
import cn.waqwb.bwcar.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author JokerQ
 * @date 2019/12/2 - 15:43
 */
@RestController
public class CarController {
    @Autowired
    private CarService carService;
    @Autowired
    private CarMakeService carMakeService;


    @RequestMapping("/sys/car/list")
    public DataGridResult findCar(QueryDTO queryDTO){
        return carService.findByPage(queryDTO);
    }


    @RequestMapping("/sys/car/del")
    public R delCar(@RequestBody List<Integer> ids){
        for (Integer id : ids) {
            carService.delCar(id);
        }
        return R.ok();
    }


    @RequestMapping("/sys/car/typeall")
    public R typeAll(){
        List<CarMake> all = carMakeService.findAll();
        return R.ok().put("sites",all);
    }


    @RequestMapping("/sys/car/info/{id}")
    public R findById(@PathVariable("id") Integer id){
        Car byId = carService.findById(id);
        return R.ok().put("car",byId);
    }


    @RequestMapping("/sys/car/save")
    public R addCar(@RequestBody Car car){
        int i = carService.addCar(car);
        return i>0?R.ok():R.error("添加失败");
    }


    @RequestMapping("/sys/car/update")
    public R updateCar(@RequestBody Car car){
        int i = carService.updateCar(car);
        return i>0?R.ok():R.error("修改失败");
    }
}
